// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "platform.h"
#include "testing.h"
#include "riot/riot_key_manager.h"
#include "testing/mock/crypto/x509_mock.h"
#include "testing/mock/keystore/keystore_mock.h"
#include "testing/engines/x509_testing_engine.h"
#include "testing/engines/ecc_testing_engine.h"
#include "testing/crypto/x509_testing.h"
#include "testing/riot/riot_core_testing.h"


TEST_SUITE_LABEL ("riot_key_manager");


/**
 * Initialize the RIoT keys structure that would be generated by RIoT Core.
 *
 * @param test The testing framework.
 * @param keys The keys to initialize.
 */
static void riot_key_manager_testing_alloc_riot_core_keys (CuTest *test, struct riot_keys *keys)
{
	keys->devid_csr = platform_malloc (RIOT_CORE_DEVID_CSR_LEN);
	CuAssertPtrNotNull (test, keys->devid_csr);

	memcpy ((uint8_t*) keys->devid_csr, RIOT_CORE_DEVID_CSR, RIOT_CORE_DEVID_CSR_LEN);
	keys->devid_csr_length = RIOT_CORE_DEVID_CSR_LEN;

	keys->devid_cert = platform_malloc (RIOT_CORE_DEVID_CERT_LEN);
	CuAssertPtrNotNull (test, keys->devid_cert);

	memcpy ((uint8_t*) keys->devid_cert, RIOT_CORE_DEVID_CERT, RIOT_CORE_DEVID_CERT_LEN);
	keys->devid_cert_length = RIOT_CORE_DEVID_CERT_LEN;

	keys->alias_key = platform_malloc (RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertPtrNotNull (test, keys->alias_key);

	memcpy ((uint8_t*) keys->alias_key, RIOT_CORE_ALIAS_KEY, RIOT_CORE_ALIAS_KEY_LEN);
	keys->alias_key_length = RIOT_CORE_ALIAS_KEY_LEN;

	keys->alias_cert = platform_malloc (RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertPtrNotNull (test, keys->alias_cert);

	memcpy ((uint8_t*) keys->alias_cert, RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN);
	keys->alias_cert_length = RIOT_CORE_ALIAS_CERT_LEN;
}

/**
 * Free the RIoT keys.
 *
 * @param test The testing framework.
 * @param keys The keys to free.
 */
static void riot_key_manager_testing_free_riot_core_keys (CuTest *test, struct riot_keys *keys)
{
	platform_free ((void*) keys->devid_csr);
	platform_free ((void*) keys->devid_cert);
	platform_free ((void*) keys->alias_key);
	platform_free ((void*) keys->alias_cert);
}

/**
 * Initialize the RIoT keys structure that would be generated by RIoT Core using static buffers.
 *
 * @param test The testing framework.
 * @param keys The keys to initialize.
 */
static void riot_key_manager_testing_static_riot_core_keys (CuTest *test, struct riot_keys *keys)
{
	keys->devid_csr = RIOT_CORE_DEVID_CSR;
	keys->devid_csr_length = RIOT_CORE_DEVID_CSR_LEN;

	keys->devid_cert = RIOT_CORE_DEVID_CERT;
	keys->devid_cert_length = RIOT_CORE_DEVID_CERT_LEN;

	keys->alias_key = RIOT_CORE_ALIAS_KEY;
	keys->alias_key_length = RIOT_CORE_ALIAS_KEY_LEN;

	keys->alias_cert = RIOT_CORE_ALIAS_CERT;
	keys->alias_cert_length = RIOT_CORE_ALIAS_CERT_LEN;
}

/*******************
 * Test cases
 *******************/

static void riot_key_manager_test_init_no_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_bad_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_BAD_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_ECC_CA_NOPL_DER, X509_CERTSS_ECC_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_ECC_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_ECC_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_intermediate_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_RSA_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_RSA_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) int_ca);

	status = testing_validate_array (X509_CERTCA_ECC_CA_NOPL_DER, int_ca->cert,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTCA_ECC_CA_NOPL_DER_LEN, int_ca->length);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_INTR_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_signed_device_id_no_root_ca (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (1), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_signed_device_id_bad_root_ca (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_BAD_KEY,
		MOCK_ARG (1), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_signed_device_id_not_signed_by_root (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_DER, X509_CERTSS_RSA_CA_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_signed_device_id_not_sign_alias_cert (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_DER, X509_CERTSS_RSA_CA_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_intermediate_signed_device_id_no_ca (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_intermediate_signed_device_id_bad_ca (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_BAD_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_intermediate_signed_device_id_not_trusted (CuTest *test)
{
	ECC_TESTING_ENGINE ecc;
	X509_TESTING_ENGINE x509;
	X509_TESTING_ENGINE x509_gen;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *int_der;
	struct ecc_private_key key;
	uint8_t *key_der;
	size_t key_length;
	struct x509_certificate ca;
	uint8_t *ca_der;
	size_t ca_der_length;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = ECC_TESTING_ENGINE_INIT (&ecc);
	CuAssertIntEquals (test, 0, status);

	status = ecc.base.generate_key_pair (&ecc.base, &key, NULL);
	CuAssertIntEquals (test, 0, status);

	status = ecc.base.get_private_key_der (&ecc.base, &key, &key_der, &key_length);
	CuAssertIntEquals (test, 0, status);

	status = X509_TESTING_ENGINE_INIT (&x509_gen);
	CuAssertIntEquals (test, 0, status);

	status = x509_gen.base.create_self_signed_certificate (&x509_gen.base, &ca, key_der, key_length,
		X509_SERIAL_NUM, X509_SERIAL_NUM_LEN, "Bad", X509_CERT_CA_NO_PATHLEN, NULL);
	CuAssertIntEquals (test, 0, status);

	status = x509_gen.base.get_certificate_der (&x509_gen.base, &ca, &ca_der, &ca_der_length);
	CuAssertIntEquals (test, 0, status);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &ca_der_length, sizeof (ca_der_length), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);

	platform_free (key_der);
	ecc.base.release_key_pair (&ecc.base, &key, NULL);
	x509.base.release_certificate (&x509_gen.base, &ca);
	X509_TESTING_ENGINE_RELEASE (&x509_gen);
	ECC_TESTING_ENGINE_RELEASE (&ecc);
}

static void riot_key_manager_test_init_riot_authentication_failed (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, 0, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));
	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_DEVID_INTR_SIGNED_CERT,
			RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN),
		MOCK_ARG (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN));

	status |= mock_expect (&x509.mock, x509.base.authenticate, &x509, X509_ENGINE_RIOT_AUTH_FAILED,
		MOCK_ARG_SAVED_ARG (0), MOCK_ARG_SAVED_ARG (1));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, keys.devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, keys.devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, keys.devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, keys.devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, keys.alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, keys.alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, keys.alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, keys.alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);
}

static void riot_key_manager_test_init_authentication_mock (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, 0, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));
	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_DEVID_INTR_SIGNED_CERT,
			RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN),
		MOCK_ARG (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN));

	status |= mock_expect (&x509.mock, x509.base.authenticate, &x509, 0, MOCK_ARG_SAVED_ARG (0),
		MOCK_ARG_SAVED_ARG (1));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_RSA_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_RSA_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) int_ca);

	status = testing_validate_array (X509_CERTCA_ECC_CA_NOPL_DER, int_ca->cert,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTCA_ECC_CA_NOPL_DER_LEN, int_ca->length);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_INTR_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);
}

static void riot_key_manager_test_init_null (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = riot_key_manager_init (NULL, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_init (&manager, NULL, &keys, &x509.base);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_init (&manager, &keystore.base, NULL, &x509.base);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, NULL);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_signed_device_id_load_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_LOAD_FAILED,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
}

static void riot_key_manager_test_init_root_ca_load_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	uint8_t *dev_id_der;
	uint8_t *ca_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_LOAD_FAILED,
		MOCK_ARG (1), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
}

static void riot_key_manager_test_init_intermediate_ca_load_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_LOAD_FAILED,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
}

static void riot_key_manager_test_init_authenticate_load_alias_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, X509_ENGINE_LOAD_FAILED,
		MOCK_ARG_NOT_NULL, MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
}

static void riot_key_manager_test_init_authenticate_init_cert_store_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509,
		X509_ENGINE_INIT_STORE_FAILED, MOCK_ARG_NOT_NULL);

	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
}

static void riot_key_manager_test_init_authenticate_add_root_ca_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, X509_ENGINE_ROOT_CA_FAILED,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
}

static void riot_key_manager_test_init_authenticate_add_intermediate_ca_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, 0, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509,
		X509_ENGINE_INTER_CA_FAILED, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
}

static void riot_key_manager_test_init_authenticate_add_device_id_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, 0, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));
	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509,
		X509_ENGINE_INTER_CA_FAILED, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_DEVID_INTR_SIGNED_CERT,
			RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN),
		MOCK_ARG (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
}

static void riot_key_manager_test_init_authenticate_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, 0, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));
	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_DEVID_INTR_SIGNED_CERT,
			RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN),
		MOCK_ARG (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN));

	status |= mock_expect (&x509.mock, x509.base.authenticate, &x509, X509_ENGINE_AUTH_FAILED,
		MOCK_ARG_SAVED_ARG (0), MOCK_ARG_SAVED_ARG (1));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_free_riot_core_keys (test, &keys);
}

static void riot_key_manager_test_init_static_no_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_static_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init_static (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_static_bad_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_static_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_BAD_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init_static (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_static_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_ECC_CA_NOPL_DER, X509_CERTSS_ECC_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_static_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init_static (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_ECC_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_ECC_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_init_static_null (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_static_riot_core_keys (test, &keys);

	status = riot_key_manager_init (NULL, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_init (&manager, NULL, &keys, &x509.base);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_init (&manager, &keystore.base, NULL, &x509.base);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, NULL);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_release_null (CuTest *test)
{
	TEST_START;

	riot_key_manager_release (NULL);
}

static void riot_key_manager_test_get_riot_keys_null (CuTest *test)
{
	const struct riot_keys *dev_keys;

	TEST_START;

	dev_keys = riot_key_manager_get_riot_keys (NULL);
	CuAssertPtrEquals (test, NULL, (struct riot_keys*) dev_keys);
}

static void riot_key_manager_test_get_root_ca_null (CuTest *test)
{
	const struct der_cert *root_ca;

	TEST_START;

	root_ca = riot_key_manager_get_root_ca (NULL);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);
}

static void riot_key_manager_test_get_intermediate_ca_null (CuTest *test)
{
	const struct der_cert *int_ca;

	TEST_START;

	int_ca = riot_key_manager_get_intermediate_ca (NULL);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);
}

static void riot_key_manager_test_verify_stored_certs_no_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_NO_SIGNED_DEVICE_ID, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_bad_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_BAD_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_NO_SIGNED_DEVICE_ID, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_ECC_CA_NOPL_DER, X509_CERTSS_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_ECC_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_ECC_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_intermediate_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_RSA_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_RSA_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) int_ca);

	status = testing_validate_array (X509_CERTCA_ECC_CA_NOPL_DER, int_ca->cert,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTCA_ECC_CA_NOPL_DER_LEN, int_ca->length);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_INTR_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_signed_device_id_no_root_ca (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (1), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_NO_ROOT_CA, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_signed_device_id_bad_root_ca (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_BAD_KEY,
		MOCK_ARG (1), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_NO_ROOT_CA, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_signed_device_id_not_signed_by_root (
	CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_DER, X509_CERTSS_RSA_CA_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_CERT_NOT_VALID, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_signed_device_id_not_sign_alias_cert (
	CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_DER, X509_CERTSS_RSA_CA_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_CERT_NOT_VALID, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_intermediate_signed_device_id_no_ca (
	CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_CERT_NOT_VALID, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_intermediate_signed_device_id_bad_ca (
	CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_BAD_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_CERT_NOT_VALID, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_intermediate_signed_device_id_not_trusted (
	CuTest *test)
{
	ECC_TESTING_ENGINE ecc;
	X509_TESTING_ENGINE x509;
	X509_TESTING_ENGINE x509_gen;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *int_der = NULL;
	struct ecc_private_key key;
	uint8_t *key_der;
	size_t key_length;
	struct x509_certificate ca;
	uint8_t *ca_der;
	size_t ca_der_length;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = ECC_TESTING_ENGINE_INIT (&ecc);
	CuAssertIntEquals (test, 0, status);

	status = ecc.base.generate_key_pair (&ecc.base, &key, NULL);
	CuAssertIntEquals (test, 0, status);

	status = ecc.base.get_private_key_der (&ecc.base, &key, &key_der, &key_length);
	CuAssertIntEquals (test, 0, status);

	status = X509_TESTING_ENGINE_INIT (&x509_gen);
	CuAssertIntEquals (test, 0, status);

	status = x509_gen.base.create_self_signed_certificate (&x509_gen.base, &ca, key_der, key_length,
		X509_SERIAL_NUM, X509_SERIAL_NUM_LEN, "Bad", X509_CERT_CA_NO_PATHLEN, NULL);
	CuAssertIntEquals (test, 0, status);

	status = x509_gen.base.get_certificate_der (&x509_gen.base, &ca, &ca_der, &ca_der_length);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_CERT_NOT_VALID, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);

	platform_free (key_der);
	ecc.base.release_key_pair (&ecc.base, &key, NULL);
	x509.base.release_certificate (&x509_gen.base, &ca);
	X509_TESTING_ENGINE_RELEASE (&x509_gen);
	ECC_TESTING_ENGINE_RELEASE (&ecc);
}

static void riot_key_manager_test_verify_stored_certs_riot_authentication_failed (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, 0, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));
	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_DEVID_INTR_SIGNED_CERT,
			RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN),
		MOCK_ARG (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN));

	status |= mock_expect (&x509.mock, x509.base.authenticate, &x509, X509_ENGINE_RIOT_AUTH_FAILED,
		MOCK_ARG_SAVED_ARG (0), MOCK_ARG_SAVED_ARG (1));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_RIOT_AUTH_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);
}

static void riot_key_manager_test_verify_stored_certs_static (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_static_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init_static (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_RSA_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_RSA_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) int_ca);

	status = testing_validate_array (X509_CERTCA_ECC_CA_NOPL_DER, int_ca->cert,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTCA_ECC_CA_NOPL_DER_LEN, int_ca->length);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_INTR_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_null (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (NULL);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_signed_device_id_load_error (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_LOAD_FAILED,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, KEYSTORE_LOAD_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_root_ca_load_error (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_LOAD_FAILED,
		MOCK_ARG (1), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, KEYSTORE_LOAD_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_intermediate_ca_load_error (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_LOAD_FAILED,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, KEYSTORE_LOAD_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_verify_stored_certs_load_alias_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, X509_ENGINE_LOAD_FAILED,
		MOCK_ARG_NOT_NULL, MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_LOAD_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);
}

static void riot_key_manager_test_verify_stored_certs_init_cert_store_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509,
		X509_ENGINE_INIT_STORE_FAILED, MOCK_ARG_NOT_NULL);

	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_INIT_STORE_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);
}

static void riot_key_manager_test_verify_stored_certs_add_root_ca_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, X509_ENGINE_ROOT_CA_FAILED,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_ROOT_CA_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);
}

static void riot_key_manager_test_verify_stored_certs_add_intermediate_ca_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, 0, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509,
		X509_ENGINE_INTER_CA_FAILED, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_INTER_CA_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);
}

static void riot_key_manager_test_verify_stored_certs_add_device_id_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, 0, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));
	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509,
		X509_ENGINE_INTER_CA_FAILED, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_DEVID_INTR_SIGNED_CERT,
			RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN),
		MOCK_ARG (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_INTER_CA_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);
}

static void riot_key_manager_test_verify_stored_certs_authenticate_error (CuTest *test)
{
	struct x509_engine_mock x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = x509_mock_init (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&x509.mock, x509.base.load_certificate, &x509, 0, MOCK_ARG_NOT_NULL,
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_ALIAS_CERT, RIOT_CORE_ALIAS_CERT_LEN),
		MOCK_ARG (RIOT_CORE_ALIAS_CERT_LEN));
	mock_expect_save_arg (&x509.mock, 0, 0);

	status |= mock_expect (&x509.mock, x509.base.init_ca_cert_store, &x509, 0,
		MOCK_ARG_NOT_NULL);
	status |= mock_expect_save_arg (&x509.mock, 0, 1);

	status |= mock_expect (&x509.mock, x509.base.add_root_ca, &x509, 0, MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));
	status |= mock_expect (&x509.mock, x509.base.add_intermediate_ca, &x509, 0,
		MOCK_ARG_SAVED_ARG (1),
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_DEVID_INTR_SIGNED_CERT,
			RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN),
		MOCK_ARG (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN));

	status |= mock_expect (&x509.mock, x509.base.authenticate, &x509, X509_ENGINE_AUTH_FAILED,
		MOCK_ARG_SAVED_ARG (0), MOCK_ARG_SAVED_ARG (1));

	status |= mock_expect (&x509.mock, x509.base.release_ca_cert_store, &x509, 0,
		MOCK_ARG_SAVED_ARG (1));
	status |= mock_expect (&x509.mock, x509.base.release_certificate, &x509, 0,
		MOCK_ARG_SAVED_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, X509_ENGINE_AUTH_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	status = x509_mock_validate_and_release (&x509);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);
}

static void riot_key_manager_test_store_signed_device_id (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.save_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN),
		MOCK_ARG (RIOT_CORE_DEVID_SIGNED_CERT_LEN));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_signed_device_id (&manager, RIOT_CORE_DEVID_SIGNED_CERT,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_signed_device_id_auth_chain_stored (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_signed_device_id (&manager, RIOT_CORE_DEVID_SIGNED_CERT,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_KEYSTORE_LOCKED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_RSA_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_RSA_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) int_ca);

	status = testing_validate_array (X509_CERTCA_ECC_CA_NOPL_DER, int_ca->cert,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTCA_ECC_CA_NOPL_DER_LEN, int_ca->length);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_INTR_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_signed_device_id_after_chain_verified (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_ECC_CA_NOPL_DER, X509_CERTSS_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_signed_device_id (&manager, RIOT_CORE_DEVID_SIGNED_CERT,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_KEYSTORE_LOCKED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_ECC_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_ECC_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_signed_device_id_null (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_signed_device_id (NULL, RIOT_CORE_DEVID_SIGNED_CERT,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_store_signed_device_id (&manager, NULL,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_store_signed_device_id (&manager, RIOT_CORE_DEVID_SIGNED_CERT,
		0);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_signed_device_id_save_error (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.save_key, &keystore, KEYSTORE_SAVE_FAILED,
		MOCK_ARG (0),
		MOCK_ARG_PTR_CONTAINS (RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN),
		MOCK_ARG (RIOT_CORE_DEVID_SIGNED_CERT_LEN));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_signed_device_id (&manager, RIOT_CORE_DEVID_SIGNED_CERT,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, KEYSTORE_SAVE_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_root_ca (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (1), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.save_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_root_ca (&manager, X509_CERTSS_RSA_CA_NOPL_DER,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_root_ca_auth_chain_stored (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_root_ca (&manager, X509_CERTSS_RSA_CA_NOPL_DER,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_KEYSTORE_LOCKED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_RSA_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_RSA_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) int_ca);

	status = testing_validate_array (X509_CERTCA_ECC_CA_NOPL_DER, int_ca->cert,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTCA_ECC_CA_NOPL_DER_LEN, int_ca->length);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_INTR_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_root_ca_after_chain_verified (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_ECC_CA_NOPL_DER, X509_CERTSS_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_root_ca (&manager, X509_CERTSS_RSA_CA_NOPL_DER,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_KEYSTORE_LOCKED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_ECC_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_ECC_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_root_ca_null (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_root_ca (NULL, X509_CERTSS_RSA_CA_NOPL_DER,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_store_root_ca (&manager, NULL,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_store_root_ca (&manager, X509_CERTSS_RSA_CA_NOPL_DER,
		0);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_root_ca_save_error (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.save_key, &keystore, KEYSTORE_SAVE_FAILED,
		MOCK_ARG (1),
		MOCK_ARG_PTR_CONTAINS (X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTSS_RSA_CA_NOPL_DER_LEN));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_root_ca (&manager, X509_CERTSS_RSA_CA_NOPL_DER,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, KEYSTORE_SAVE_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_intermediate_ca (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.save_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_intermediate_ca (&manager, X509_CERTCA_ECC_CA_NOPL_DER,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_intermediate_ca_auth_chain_stored (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_intermediate_ca (&manager, X509_CERTCA_ECC_CA_NOPL_DER,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_KEYSTORE_LOCKED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_RSA_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_RSA_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) int_ca);

	status = testing_validate_array (X509_CERTCA_ECC_CA_NOPL_DER, int_ca->cert,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTCA_ECC_CA_NOPL_DER_LEN, int_ca->length);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_INTR_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_intermediate_ca_after_chain_verified (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_ECC_CA_NOPL_DER, X509_CERTSS_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_intermediate_ca (&manager, X509_CERTCA_ECC_CA_NOPL_DER,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_KEYSTORE_LOCKED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_ECC_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_ECC_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_intermediate_ca_null (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_intermediate_ca (NULL, X509_CERTCA_ECC_CA_NOPL_DER,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_store_intermediate_ca (&manager, NULL,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = riot_key_manager_store_intermediate_ca (&manager, X509_CERTCA_ECC_CA_NOPL_DER,
		0);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_store_intermediate_ca_save_error (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.save_key, &keystore, KEYSTORE_SAVE_FAILED,
		MOCK_ARG (2),
		MOCK_ARG_PTR_CONTAINS (X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN),
		MOCK_ARG (X509_CERTCA_ECC_CA_NOPL_DER_LEN));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_store_intermediate_ca (&manager, X509_CERTCA_ECC_CA_NOPL_DER,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, KEYSTORE_SAVE_FAILED, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_release_riot_keys (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;
	uint8_t *ca_der = NULL;
	uint8_t *int_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_SIGNED_CERT, RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_ECC_CA_NOPL_DER, X509_CERTSS_ECC_CA_NOPL_DER_LEN);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_ECC_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (2), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_verify_stored_certs (&manager);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_ECC_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_ECC_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_release_riot_keys_null (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (NULL, dev_keys);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_erase_all_certificates (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der;
	uint8_t *ca_der;
	uint8_t *int_der;

	TEST_START;

	dev_id_der = platform_malloc (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertPtrNotNull (test, dev_id_der);

	ca_der = platform_malloc (X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, ca_der);

	int_der = platform_malloc (X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertPtrNotNull (test, int_der);

	memcpy (dev_id_der, RIOT_CORE_DEVID_INTR_SIGNED_CERT, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	memcpy (ca_der, X509_CERTSS_RSA_CA_NOPL_DER, X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	memcpy (int_der, X509_CERTCA_ECC_CA_NOPL_DER, X509_CERTCA_ECC_CA_NOPL_DER_LEN);

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (0),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN,
		sizeof (RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (1),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &ca_der, sizeof (ca_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTSS_RSA_CA_NOPL_DER_LEN,
		sizeof (X509_CERTSS_RSA_CA_NOPL_DER_LEN), -1);

	status |= mock_expect (&keystore.mock, keystore.base.load_key, &keystore, 0, MOCK_ARG (2),
		MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &int_der, sizeof (int_der), -1);
	status |= mock_expect_output (&keystore.mock, 2, &X509_CERTCA_ECC_CA_NOPL_DER_LEN,
		sizeof (X509_CERTCA_ECC_CA_NOPL_DER_LEN), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, 0, MOCK_ARG (0));
	status |= mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, 0, MOCK_ARG (1));
	status |= mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, 0, MOCK_ARG (2));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_erase_all_certificates (&manager);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) root_ca);

	status = testing_validate_array (X509_CERTSS_RSA_CA_NOPL_DER, root_ca->cert,
		X509_CERTSS_RSA_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTSS_RSA_CA_NOPL_DER_LEN, root_ca->length);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrNotNull (test, (struct der_cert*) int_ca);

	status = testing_validate_array (X509_CERTCA_ECC_CA_NOPL_DER, int_ca->cert,
		X509_CERTCA_ECC_CA_NOPL_DER_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, X509_CERTCA_ECC_CA_NOPL_DER_LEN, int_ca->length);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_INTR_SIGNED_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_INTR_SIGNED_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_erase_all_certificates_none_stored (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	const struct riot_keys *dev_keys;
	const struct der_cert *root_ca;
	const struct der_cert *int_ca;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, 0, MOCK_ARG (0));
	status |= mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, 0, MOCK_ARG (1));
	status |= mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, 0, MOCK_ARG (2));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_erase_all_certificates (&manager);
	CuAssertIntEquals (test, 0, status);

	dev_keys = riot_key_manager_get_riot_keys (&manager);
	CuAssertTrue (test, (&keys != dev_keys));

	riot_key_manager_release_riot_keys (&manager, dev_keys);

	root_ca = riot_key_manager_get_root_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) root_ca);

	int_ca = riot_key_manager_get_intermediate_ca (&manager);
	CuAssertPtrEquals (test, NULL, (struct der_cert*) int_ca);

	status = testing_validate_array (RIOT_CORE_DEVID_CSR, dev_keys->devid_csr,
		RIOT_CORE_DEVID_CSR_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CSR_LEN, dev_keys->devid_csr_length);

	status = testing_validate_array (RIOT_CORE_DEVID_CERT, dev_keys->devid_cert,
		RIOT_CORE_DEVID_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_DEVID_CERT_LEN, dev_keys->devid_cert_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_KEY, dev_keys->alias_key,
		RIOT_CORE_ALIAS_KEY_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_KEY_LEN, dev_keys->alias_key_length);

	status = testing_validate_array (RIOT_CORE_ALIAS_CERT, dev_keys->alias_cert,
		RIOT_CORE_ALIAS_CERT_LEN);
	CuAssertIntEquals (test, 0, status);
	CuAssertIntEquals (test, RIOT_CORE_ALIAS_CERT_LEN, dev_keys->alias_cert_length);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_erase_all_certificates_null (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_erase_all_certificates (NULL);
	CuAssertIntEquals (test, RIOT_KEY_MANAGER_INVALID_ARGUMENT, status);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_erase_all_certificates_device_id_error (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, KEYSTORE_ERASE_FAILED,
		MOCK_ARG (0));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_erase_all_certificates (&manager);
	CuAssertIntEquals (test, KEYSTORE_ERASE_FAILED, status);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_erase_all_certificates_root_ca_error (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, 0, MOCK_ARG (0));
	status |= mock_expect (&keystore.mock, keystore.base.erase_key, &keystore,
		KEYSTORE_ERASE_FAILED, MOCK_ARG (1));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_erase_all_certificates (&manager);
	CuAssertIntEquals (test, KEYSTORE_ERASE_FAILED, status);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}

static void riot_key_manager_test_erase_all_certificates_intermediate_ca_error (CuTest *test)
{
	X509_TESTING_ENGINE x509;
	struct keystore_mock keystore;
	struct riot_keys keys;
	struct riot_key_manager manager;
	int status;
	uint8_t *dev_id_der = NULL;

	TEST_START;

	status = X509_TESTING_ENGINE_INIT (&x509);
	CuAssertIntEquals (test, 0, status);

	status = keystore_mock_init (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_testing_alloc_riot_core_keys (test, &keys);

	status = mock_expect (&keystore.mock, keystore.base.load_key, &keystore, KEYSTORE_NO_KEY,
		MOCK_ARG (0), MOCK_ARG_NOT_NULL, MOCK_ARG_NOT_NULL);
	status |= mock_expect_output (&keystore.mock, 1, &dev_id_der, sizeof (dev_id_der), -1);

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_init (&manager, &keystore.base, &keys, &x509.base);
	CuAssertIntEquals (test, 0, status);

	status = mock_validate (&keystore.mock);
	CuAssertIntEquals (test, 0, status);

	status = mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, 0, MOCK_ARG (0));
	status |= mock_expect (&keystore.mock, keystore.base.erase_key, &keystore, 0, MOCK_ARG (1));
	status |= mock_expect (&keystore.mock, keystore.base.erase_key, &keystore,
		KEYSTORE_ERASE_FAILED, MOCK_ARG (2));

	CuAssertIntEquals (test, 0, status);

	status = riot_key_manager_erase_all_certificates (&manager);
	CuAssertIntEquals (test, KEYSTORE_ERASE_FAILED, status);

	status = keystore_mock_validate_and_release (&keystore);
	CuAssertIntEquals (test, 0, status);

	riot_key_manager_release (&manager);

	X509_TESTING_ENGINE_RELEASE (&x509);
}


TEST_SUITE_START (riot_key_manager);

TEST (riot_key_manager_test_init_no_signed_device_id);
TEST (riot_key_manager_test_init_bad_signed_device_id);
TEST (riot_key_manager_test_init_signed_device_id);
TEST (riot_key_manager_test_init_intermediate_signed_device_id);
TEST (riot_key_manager_test_init_signed_device_id_no_root_ca);
TEST (riot_key_manager_test_init_signed_device_id_bad_root_ca);
TEST (riot_key_manager_test_init_signed_device_id_not_signed_by_root);
TEST (riot_key_manager_test_init_signed_device_id_not_sign_alias_cert);
TEST (riot_key_manager_test_init_intermediate_signed_device_id_no_ca);
TEST (riot_key_manager_test_init_intermediate_signed_device_id_bad_ca);
TEST (riot_key_manager_test_init_intermediate_signed_device_id_not_trusted);
TEST (riot_key_manager_test_init_riot_authentication_failed);
TEST (riot_key_manager_test_init_authentication_mock);
TEST (riot_key_manager_test_init_null);
TEST (riot_key_manager_test_init_signed_device_id_load_error);
TEST (riot_key_manager_test_init_root_ca_load_error);
TEST (riot_key_manager_test_init_intermediate_ca_load_error);
TEST (riot_key_manager_test_init_authenticate_load_alias_error);
TEST (riot_key_manager_test_init_authenticate_init_cert_store_error);
TEST (riot_key_manager_test_init_authenticate_add_root_ca_error);
TEST (riot_key_manager_test_init_authenticate_add_intermediate_ca_error);
TEST (riot_key_manager_test_init_authenticate_add_device_id_error);
TEST (riot_key_manager_test_init_authenticate_error);
TEST (riot_key_manager_test_init_static_no_signed_device_id);
TEST (riot_key_manager_test_init_static_bad_signed_device_id);
TEST (riot_key_manager_test_init_static_signed_device_id);
TEST (riot_key_manager_test_init_static_null);
TEST (riot_key_manager_test_release_null);
TEST (riot_key_manager_test_get_riot_keys_null);
TEST (riot_key_manager_test_get_root_ca_null);
TEST (riot_key_manager_test_get_intermediate_ca_null);
TEST (riot_key_manager_test_verify_stored_certs_no_signed_device_id);
TEST (riot_key_manager_test_verify_stored_certs_bad_signed_device_id);
TEST (riot_key_manager_test_verify_stored_certs_signed_device_id);
TEST (riot_key_manager_test_verify_stored_certs_intermediate_signed_device_id);
TEST (riot_key_manager_test_verify_stored_certs_signed_device_id_no_root_ca);
TEST (riot_key_manager_test_verify_stored_certs_signed_device_id_bad_root_ca);
TEST (riot_key_manager_test_verify_stored_certs_signed_device_id_not_signed_by_root);
TEST (riot_key_manager_test_verify_stored_certs_signed_device_id_not_sign_alias_cert);
TEST (riot_key_manager_test_verify_stored_certs_intermediate_signed_device_id_no_ca);
TEST (riot_key_manager_test_verify_stored_certs_intermediate_signed_device_id_bad_ca);
TEST (riot_key_manager_test_verify_stored_certs_intermediate_signed_device_id_not_trusted);
TEST (riot_key_manager_test_verify_stored_certs_riot_authentication_failed);
TEST (riot_key_manager_test_verify_stored_certs_static);
TEST (riot_key_manager_test_verify_stored_certs_null);
TEST (riot_key_manager_test_verify_stored_certs_signed_device_id_load_error);
TEST (riot_key_manager_test_verify_stored_certs_root_ca_load_error);
TEST (riot_key_manager_test_verify_stored_certs_intermediate_ca_load_error);
TEST (riot_key_manager_test_verify_stored_certs_load_alias_error);
TEST (riot_key_manager_test_verify_stored_certs_init_cert_store_error);
TEST (riot_key_manager_test_verify_stored_certs_add_root_ca_error);
TEST (riot_key_manager_test_verify_stored_certs_add_intermediate_ca_error);
TEST (riot_key_manager_test_verify_stored_certs_add_device_id_error);
TEST (riot_key_manager_test_verify_stored_certs_authenticate_error);
TEST (riot_key_manager_test_store_signed_device_id);
TEST (riot_key_manager_test_store_signed_device_id_auth_chain_stored);
TEST (riot_key_manager_test_store_signed_device_id_after_chain_verified);
TEST (riot_key_manager_test_store_signed_device_id_null);
TEST (riot_key_manager_test_store_signed_device_id_save_error);
TEST (riot_key_manager_test_store_root_ca);
TEST (riot_key_manager_test_store_root_ca_auth_chain_stored);
TEST (riot_key_manager_test_store_root_ca_after_chain_verified);
TEST (riot_key_manager_test_store_root_ca_null);
TEST (riot_key_manager_test_store_root_ca_save_error);
TEST (riot_key_manager_test_store_intermediate_ca);
TEST (riot_key_manager_test_store_intermediate_ca_auth_chain_stored);
TEST (riot_key_manager_test_store_intermediate_ca_after_chain_verified);
TEST (riot_key_manager_test_store_intermediate_ca_null);
TEST (riot_key_manager_test_store_intermediate_ca_save_error);
TEST (riot_key_manager_test_release_riot_keys);
TEST (riot_key_manager_test_release_riot_keys_null);
TEST (riot_key_manager_test_erase_all_certificates);
TEST (riot_key_manager_test_erase_all_certificates_none_stored);
TEST (riot_key_manager_test_erase_all_certificates_null);
TEST (riot_key_manager_test_erase_all_certificates_device_id_error);
TEST (riot_key_manager_test_erase_all_certificates_root_ca_error);
TEST (riot_key_manager_test_erase_all_certificates_intermediate_ca_error);

TEST_SUITE_END;
